package com.situ.web.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.situ.web.pojo.entity.Banji;
import com.situ.web.pojo.query.BanjiQuery;
import com.situ.web.pojo.vo.BanjiCountVO;
import com.situ.web.service.IBanjiService;
import com.situ.web.service.impl.BanjiServiceImpl;
import com.situ.web.util.JSONUtil;
import com.situ.web.util.LayUITableResult;
import com.situ.web.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/banji")
public class BanjiServlet extends HttpServlet {
    private IBanjiService banjiService = new BanjiServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method) {
            case "selectBanjiCount":
                selectBanjiCount(req, resp);
                break;
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "deleteById":
                deleteById(req, resp);
                break;
            case "deleteAll":
                deleteAll(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "selectById":
                selectById(req, resp);
                break;
            case "update":
                update(req, resp);
                break;
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BanjiServlet.update");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String address = req.getParameter("address");
        String time = req.getParameter("time");
        Banji banji = new Banji(Integer.parseInt(id), name, address, time);
        banjiService.update(banji);
        JSONUtil.toJSON(resp, Result.ok("更新成功"));
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BanjiServlet.selectById");
        String id = req.getParameter("id");
        Banji banji = banjiService.selectById(Integer.parseInt(id));
        JSONUtil.toJSON(resp, Result.ok(banji));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BanjiServlet.add");
        String name = req.getParameter("name");
        String address = req.getParameter("address");
        String time = req.getParameter("time");
        Banji banji = new Banji(null, name, address, time);
        banjiService.add(banji);
        JSONUtil.toJSON(resp, Result.ok("添加成功"));
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BanjiServlet.deleteAll");
        String[] ids = req.getParameterValues("ids[]");
        banjiService.deleteAll(ids);
        JSONUtil.toJSON(resp, Result.ok("删除成功"));
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BanjiServlet.deleteById");
        String id = req.getParameter("id");
        banjiService.deleteById(Integer.parseInt(id));
        /*Result result = new Result();
        result.setCode(0);
        result.setMsg("删除成功");
        JSONUtil.toJSON(resp, result);*/
        JSONUtil.toJSON(resp, Result.ok("删除成功"));
    }

    // /banji?method=selectByPage&page=1&limit=10
    // /banji?method=selectByPage&page=1&limit=10&name=zhansgsan&address=5&time=2014
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BanjiServlet.selectByPage");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String name = req.getParameter("name");
        String address = req.getParameter("address");
        String time = req.getParameter("time");
        BanjiQuery banjiQuery = new BanjiQuery(Integer.parseInt(page), Integer.parseInt(limit), name, address, time);
        LayUITableResult layUITableResult = banjiService.selectByPage(banjiQuery);

        JSONUtil.toJSON(resp, layUITableResult);
    }

    private void selectBanjiCount(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("BanjiServlet.selectBanjiCount");
        List<BanjiCountVO> list = banjiService.selectBanjiCount();

        resp.setContentType("text/html;charset=UTF-8");
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(resp.getWriter(), list);
    }
}